package 懒汉式;

/**
 * @projectName: com.xiayuanxing.designPattern
 * @className: SingletonTest03
 * @author: xiayuanxing
 * @description: 懒汉式（线程安全,同步代码块）
 * @date: 2021/12/16 9:47
 * @version: 1.0
 */
public class SingletonTest05 {

    // 不推荐使用

    public static void main(String[] args) {
        System.out.println("懒汉式3，线程安全~");
        Singletion5 instance1 = Singletion5.getInstance();
        Singletion5 instance2 = Singletion5.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println("instance1.hashCode="+instance1.hashCode());
        System.out.println("instance2.hashCode="+instance2.hashCode());
    }
}

class Singletion5{

    //6666
    private static Singletion5 instance;

    //1.构造器私有化
    private Singletion5(){

    };

    //2.提供一个静态的公共方法，加入同步处理的代码块，解决线程问题
    public static  Singletion5 getInstance(){
        if (instance == null){
            synchronized (Singletion5.class){
                instance = new Singletion5();
            }
        }
        return instance;
    }
}